<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <!-- 表单 -->
        <el-form class="login_form" :model="loginForm" :rules="rules" ref="loginForms">
          <h1>Hello!</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <!-- 用户名 -->
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" :prefix-icon="User">
            </el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" :prefix-icon="Lock" show-password></el-input>
          </el-form-item>
          <!-- 登录 -->
          <el-form-item>
            <el-button class="login_btn" type="primary" @click="login">登录</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { Lock, User } from "@element-plus/icons-vue";
import { reactive, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import useUserStore from "@/store/modules/user.ts";
import { ElMessage, ElNotification } from "element-plus";
import { getTime } from "../../utils/time";

let useStore = useUserStore();
let $router = useRouter();
let $route = useRoute();

// 登录表单
let loginForms = ref();

// 登录表单信息
let loginForm = reactive({
  username: "",
  password: "",
});

// 登录按钮
const login = async () => {
  await loginForms.value.validate();
  try {
    await useStore.userLogin(loginForm);
    let redirect: any = $route.query.redirect;
    $router.push({ path: redirect || '/' });
    ElNotification({
      type: "success",
      message: "欢迎回来",
      title: `${getTime()}好！`,
    });
  } catch (error) {
    ElMessage({
      type: "error",
      message: (error as Error).message,
    });
  }
};

const validatorUserName = (rulr: any, value: any, callback: any) => {
  if (value.length >= 5) {
    callback();
  } else {
    callback(new Error("账号长度至少五位"));
  }
};

// 登录校验规则
const rules = {
  username: [
    // {
    //   required: true,
    //   min: 4,
    //   max: 10,
    //   message: "账号长度至少四位",
    //   trigger: "change",
    // },
    {
      trigger: "change",
      validator: validatorUserName,
    },
  ],
  password: [
    {
      required: true,
      min: 6,
      max: 15,
      message: "密码长度至少六位",
      trigger: "change",
    },
  ],
};
</script>

<style lang="scss" scoped>
.login_container {
  height: 100vh;
  background: url("@/assets/images/background.jpg") no-repeat;
  background-size: cover;
  position: relative;

  .el-col {
    height: 100vh;

    .login_form {
      position: relative;
      top: 50%;
      left: 10%;
      transform: translateY(-50%);
      padding: 30px;
      width: 70%;
      background: url("@/assets/images/login_form.png") no-repeat;
      background-color: rgba($color: #000, $alpha: 0.2);
      background-size: cover;
      border-radius: 5%;

      color: #fff;

      h1 {
        font-size: 40px;
        margin: 10px 0;
      }

      h2 {
        font-size: 16px;
        margin: 5px 0 20px 0;
      }

      .login_btn {
        width: 100%;
        margin: auto;
      }
    }
  }
}
</style>